GBML manual / Camera

The camera

The screen is a 240x160 window onto a world that can be much bigger, and the camera decides which part of that world the window shows.

A window onto the world

The GBA screen is 240 pixels wide and 160 tall. That is small, and a room is usually much larger. A room's size in pixels is room_width() by room_height(). So the screen shows only a part of the room at any moment, and the camera decides which part.

Every position in the game is a world position: x and y measured from the top-left corner of the room, where (0, 0) is. An instance at world (100, 0) is 100 pixels right of the room's left edge, on the room's top edge, whether or not the camera is looking there. The camera has a world position too. It is the world point where the view begins: the top-left corner of what the screen shows. With the camera at (0, 0), the screen shows the world from x = 0 to 240 and y = 0 to 160. A room the size of the screen shows exactly with the default camera. (Something at world (x, y) appears on the screen at (x - camera_x(), y - camera_y()).)

Move the camera and everything in the world slides across the screen together: instances, room tiles, backgrounds and text drawn with draw_text. Things that are drawn fixed to the screen do not move. That covers everything drawn in a Draw GUI event, and the _anchored drawing functions. This is how a health bar stays in its corner while the room scrolls behind it.

Following something

Nothing follows anything until you say so. camera_follow(target) makes the camera follow one instance, usually the player:

// Create event of the player
camera_follow(self);

Following is eased. The camera does not lock onto the target. Each frame it closes part of the gap between where it is and where the target is, so it trails a little behind and settles when the target stops. That looks smooth and it is the usual feel for a game.

The easing is done in whole pixels on purpose. A moving sprite and the camera each round to a whole pixel, and easing a fractional camera position makes moving sprites shimmer by one pixel every frame. The engine avoids that, so you do not need to.

Making the follow feel right

Three more settings change how the followed camera sits on its target. All three are whole pixels, all three start at 0 (which is how the camera has always been), and all three stay until you change them.

Picture the camera looking at a spot. The offset and the look-ahead move that spot away from the target. The dead zone decides whether the camera bothers to follow it when the spot moves a little. Ease (see above) decides how quickly it catches up once it does.

Setting it without code

You do not have to write any of this. Room Properties has a Camera section (and a world has the same section in World Properties), with a row for each thing on this page:

RowWhat it does
Followswho the camera follows from the start. A world starts on its player
Stops at edgeskeeps the screen from showing past the room's edge (this is the same tickbox that used to be in Transitions and Values)
Easehow tightly it follows, from soft to tight. The last notch is no lag. Tick "Ease up/down separately" to give the vertical its own slider
Dead zonethe box, width and height
Look-aheadthe distance, and how fast it swings when the player turns round
Offsetacross and down
Snap on arrivalwhether the camera jumps to its target when the room starts (the normal way), or eases there from where it was

The settings are applied when the room starts, before anything in your code runs. That is the whole rule for how the panel and your code get along: code always wins. If a Create event says camera_follow(self) or camera_dead_zone(0, 0), that call happens after the panel did its part, so it changes the camera and the panel does not undo it. A room that sets nothing in the panel leaves the camera alone, so a game written entirely in code plays exactly as it did before the panel existed.

A room that sets anything in its Camera section starts with all of it: whatever you left alone is the normal value (ease 0.08, no dead zone, no look-ahead, no offset, snap on), not what the last room happened to leave behind. A room that sets nothing leaves the camera as it finds it.

In a world, the settings are applied once, when the world is entered. Walking from one part of the world into the next never restarts them, and never undoes what your code set in the meantime.

Placing it yourself

camera_set(x, y) stops following and puts the top-left corner of the view at that world point at once. It suits a fixed shot: looking at a door before a cutscene, or a boss arena's center. The point you give is the top-left corner of the view, so to put a point (cx, cy) in the middle of the screen, subtract half a screen:

// Step event of a boss arena's door, once the player is inside
camera_set(160 - screen_width() / 2, 120 - screen_height() / 2);   // centre the view on (160, 120)

camera_x() and camera_y() read where the view begins now.

Keeping it inside the room

You normally do not want the screen to show the empty space past a room's edge. Bounds are a rectangle the camera is not allowed to leave. When a room loads, the bounds are set for you: the view may begin anywhere from the room's top-left corner (0, 0) to (room_width() - screen_width(), room_height() - screen_height()), so the screen edge stops at the room edge. A room smaller than the screen is shown centered on it: the camera is pulled back inside the bounds whenever a room loads, and in that case camera_x() can be below 0. A room can also be set not to clamp the camera at all.

Bounds apply to a followed camera and to camera_set.

Shaking

camera_shake(power, frames) adds a random jitter of up to power pixels, fading to nothing over frames frames. It is for impacts and explosions. The jitter is a jolt on top of the normal camera position, so following carries on normally underneath it. A power or frames of 0 or less stops a shake at once.

// Step event of a boss that slams the ground
if (btn_pressed(BTN_B)) {
    camera_shake(4, 20);   // up to 4 pixels of jitter, fading out over 20 frames
}

Everything in this category

EntryWhat it is
camera_follow, camera_unfollowstart and stop following an instance
camera_follow_easehow tightly it follows
camera_dead_zone, camera_look_ahead, camera_offseta box the target can move in, looking ahead of it, and shifting the middle of the view
camera_snapjump to the target now
camera_setplace it at a point
camera_bounds, camera_bounds_clearthe rectangle it may not leave
camera_x, camera_ywhere it is
camera_shakea temporary jolt

There is one camera for the whole game, so the settings above are global and not per room. Each room load sets the bounds again, applies the room's Camera section (if it sets anything) and snaps the camera to whatever it is following. Rooms covers the rest of what a room load does.

What it cannot do

See also: Drawing and the draw orderBackgroundsRoomscamera_followcamera_boundscamera_dead_zonecamera_look_aheadcamera_offsetroom_widthZoomable backgrounds